package com.rytong.tools.ui;

import java.util.Timer;
import java.util.TimerTask;
import com.rytong.tools.utils.Utils;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LPProgressDialog extends Dialog {

    public static Progress PROGRESS;
    static String CONTEXT;
    static LPProgressDialog PROGRESSDIALOG;

    public LPProgressDialog(Activity baseView, String content,int theme) {
        // TODO Auto-generated constructor stub
        super(baseView,theme);
        PROGRESS = new Progress(baseView);
        ImageView ii = new ImageView(baseView);
        Bitmap bitmap = Utils.getBitmapFromResources(baseView,"temp");
        ii.setBackgroundDrawable(new BitmapDrawable(bitmap));
        CONTEXT = content;
        LinearLayout ll = new LinearLayout(baseView);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.addView(PROGRESS);
        ll.addView(ii);
        setContentView(ll);
    }

    static class Progress extends TextView {
        static Bitmap PROGRESSP;
        static Bitmap PROGRESSB;
        static Matrix MAT;
        static Timer timer;
        private float degree_;
        final int textSize_ = 10;

        public Progress(Context context) {
            super(context);
            // TODO Auto-generated constructor stub+
            // 进度提示中间的圈圈图片宽度相对于320X480的界面定的大小
            final int wpp = 30;
            // 进度提示框背景宽度相对于320X480的界面定的大小
            final int wpb = 120;
            if (null == PROGRESSP) {
                PROGRESSP = Utils.getBitmapFromResources(getContext(), "progress1");
                final int w = Utils.getScaledValueX(wpp);
                final int h = w * PROGRESSP.getHeight() / PROGRESSP.getWidth();
                PROGRESSP = Bitmap.createScaledBitmap(PROGRESSP, w, h, true);
            }
            if (null == PROGRESSB) {
                PROGRESSB =  Utils.getBitmapFromResources(getContext(),"progressb");
                final int w = Utils.getScaledValueX(wpb);
                final int h = w * PROGRESSB.getHeight() / PROGRESSB.getWidth();
                PROGRESSB = Bitmap.createScaledBitmap(PROGRESSB, w, h, true);
            }
            if (null == MAT) {
                MAT = new Matrix();
            }
            if (null == timer) {
                timer = new Timer();
            }
        }

        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            setMeasuredDimension(PROGRESSB.getWidth(), PROGRESSB.getHeight());
        }

        public void onDraw(Canvas canvas) {
            String waitMessage = "";
            String valuePercent = "";
            // 设置转圈的角度和中心点
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(0xFFFFFFFF);
            paint.setFilterBitmap(true);

            // 设置字体为粗体
            // paint.setFakeBoldText(true);
            int x = Utils.getScaledValueX(5);// (PROGRESSB.getWidth() - PROGRESSP.getWidth()) >> 1;
            int yBmp = (PROGRESSB.getHeight() - PROGRESSP.getHeight()) >> 1;
            MAT.setRotate(degree_, PROGRESSP.getWidth() >> 1, PROGRESSP.getHeight() >> 1);
            MAT.postTranslate(x, yBmp);
            degree_ = (float) (degree_ + 22.5);
            if (degree_ == 360)
                degree_ = 0;
            // 绘制背景图片
            canvas.drawBitmap(PROGRESSB, 0, 0, paint);
            // 绘制进度旋转图片
            canvas.drawBitmap(PROGRESSP, MAT, paint);
            if (CONTEXT.contains("%")) {
                try {
                    if (CONTEXT.length() > waitMessage.length()) {
                        valuePercent = CONTEXT.substring(waitMessage.length());
                        CONTEXT = CONTEXT.substring(0, waitMessage.length());
                    }
                } catch (Exception e) {
                    Utils.printException(e);
                }
            }
            paint.setTextSize((PROGRESSB.getHeight() << 1) / 5);
            yBmp = (int) (paint.getTextSize() + PROGRESSB.getHeight()) >> 1;
            canvas.drawText(CONTEXT, (x << 1) + PROGRESSP.getWidth(), yBmp, paint);
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    LPProgressDialog.Progress.this.postInvalidate();
                }

            }, 100);
        }
    }

    public void setMessage(String loadingStatus) {
        // TODO Auto-generated method stub
        CONTEXT = loadingStatus;
    }

}
